package org.ifg.bn.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ifg.bn.dto.User;
import org.ifg.bn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys")
public class UserController {
	
	private static final Log logger = LogFactory.getLog(UserController.class);

	@Autowired
	private UserService userService;
	
	/**
	 * 动态页面跳转控制器
	 * 
	 * @param formName
	 * @return
	 */
	@RequestMapping(value="/{forName}")
	public String dynamicForm(@PathVariable String forName){
		return forName;
	}
	
	/**
	 * 进入登录页面
	 * @return
	 */
	@RequestMapping(value={"/tologin.do" , "/"})
	public String logform(HttpServletRequest request,HttpSession session) {
		//获取web项目的路径
		String allPath = request.getRequestURL().toString();
		String appName = allPath.substring(0, allPath.indexOf(request.getServletPath()));
		session.setAttribute("appName", appName);
		return "login";
	}
	
	/**
	 * 登录操作
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/login.do", method=RequestMethod.POST)
	@ResponseBody
	public String login(User user,HttpServletRequest request,HttpSession session){
		logger.info("开始登录");
		User resUser = userService.login(user);
		if (resUser != null){
			session.setAttribute("loginUser", resUser);
			return "true";
		}else{
			logger.error("登录失败");
			return "false";
		}
	}

}
